{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.4 64-bit ('venv': virtualenv)",
   "metadata": {
    "interpreter": {
     "hash": "fb690ddc510741cfa3f89f1158f67c470f636acbcc9f60182cea5200cd793538"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "ImportError",
     "evalue": "cannot import name 'urlopen' from 'urllib3' (/Volumes/BC-MAC-COLLIN/Workspace/Projects/linn-mobile-control/linn-mobile-automation-agent/venv/lib/python3.7/site-packages/urllib3/__init__.py)",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-962e83cba661>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcsv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpytz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0murllib3\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0murlopen\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m: cannot import name 'urlopen' from 'urllib3' (/Volumes/BC-MAC-COLLIN/Workspace/Projects/linn-mobile-control/linn-mobile-automation-agent/venv/lib/python3.7/site-packages/urllib3/__init__.py)"
     ]
    }
   ],
   "source": [
    "import uiautomator2 as u2  \n",
    "import multiprocessing\n",
    "import datetime\n",
    "import random\n",
    "import csv\n",
    "import pytz\n",
    "from urllib2 import urlopen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<pytz._CountryNameDict at 0x7ff970053050>"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "\n",
    "my_ip = urlopen('http://ip.42.pl/raw').read()\n",
    "print('ip.42.pl', my_ip)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "arg: 1\narg: two\narg: 3\nfirst_name cai\nlast_name peng\nage 20\n"
     ]
    }
   ],
   "source": [
    "def check_args(*args, **kwargs):\n",
    "    for value in args:\n",
    "        print (\"arg:\", value)\n",
    "    for key in kwargs:\n",
    "        print(key, kwargs[key])\n",
    "\n",
    "check_args(1, \"two\", 3, first_name=\"cai\", last_name=\"peng\", age=20)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "not None\n"
     ]
    }
   ],
   "source": [
    "operator_data = None\n",
    "\n",
    "if not operator_data is None:\n",
    "    print(\"not None\")\n",
    "else:\n",
    "    print(\"None\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "app_name 微信\n{'app_name': '微信'}\napp_name\n32768\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "with open('../mappsetting/wx_7.0.13.csv', 'r') as f:\n",
    "    dict_reader = csv.DictReader(f)\n",
    "    settings = dict()\n",
    "    for item in dict_reader:\n",
    "        settings[item['name']] = item['id']\n",
    "        print(item['name'], item['id'])\n",
    "    print(settings)\n",
    "    for (name, value) in settings.items():\n",
    "        print(name)\n",
    "\n",
    "        print(2 ** 15)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'dict' object has no attribute 'has_key'",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-87-9dc760c0c052>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mdic\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mdic\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"name\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"caipeng\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"wangyuan\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"caipeng\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdic\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhas_key\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'name'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0mdic\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute 'has_key'"
     ]
    }
   ],
   "source": [
    "dic = dict()\n",
    "dic[\"name\"] = [\"caipeng\", \"wangyuan\", \"caipeng\"]\n",
    "dic.has_key('name')\n",
    "dic\n",
    "\n",
    "now = datetime.datetime.now()\n",
    "odd_now = now + datetime.timedelta(minutes=30)\n",
    "odd_now\n",
    "\n",
    "def randBonus(min, max, total, num):\n",
    "    print(min, max, total, num)\n",
    "    total = int(total)\n",
    "    num = int(num)\n",
    "    #min = 1\n",
    "     \n",
    "    if num < 1:\n",
    "        return\n",
    "    if num == 1:\n",
    "        print(\"第%d个人拿到红包数:%s\" % (num,total))\n",
    "        return\n",
    "    i = 1\n",
    "    totalMoney = total\n",
    "    while(i < num):\n",
    "        max = totalMoney - min*(num - i)\n",
    "        k = int((num-i)/2)\n",
    "        if num -i <= 2:\n",
    "            k = num -i\n",
    "        max = max/k\n",
    "        monney = random.randint(int(min), int(max))\n",
    "        #monney = int(monney)\n",
    "        totalMoney = totalMoney - monney\n",
    "        print(\"第%d个人拿到红包为:%s, 余额:%s\"%(i,monney,totalMoney))\n",
    "        i += 1\n",
    " \n",
    "    print(\"第%d个人拿到红包为:%s, 余额:%s\"%(i,totalMoney,0))\n",
    "\n",
    "randBonus(33, 66, 1320, 40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "41\n33\n10\n13\n10\n10\n56\n62\n50\n56\n10\n55\n10\n42\n10\n41\n31\n36\n33\n34\n63\n27\n57\n47\n44\n43\n24\n39\n54\n10\n10\n16\n46\n38\n32\n20\n10\n45\n42\n10\n"
     ]
    }
   ],
   "source": [
    "def hb_generator(hongbao_num, money_sum):\n",
    "    hongbao_remain = int(hongbao_num)\n",
    "    for i in range(int(hongbao_num) -1):\n",
    "        min = 10\n",
    "        max = money_sum/hongbao_remain*2\n",
    "        money = int(round(random.uniform(0, max), 2))\n",
    "        hongbao_money = money if money > min else min\n",
    "        hongbao_remain -= 1\n",
    "        money_sum -= hongbao_money\n",
    "        yield hongbao_money\n",
    "    yield round(int(money_sum), 2)\n",
    "\n",
    "for i in hb_generator(40, 1320):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "43\n32\n20\n46\n20\n58\n20\n40\n20\n34\n20\n34\n20\n26\n20\n20\n63\n22\n65\n59\n52\n35\n50\n20\n28\n20\n38\n20\n39\n38\n45\n45\n21\n20\n20\n20\n28\n20\n47\n32\n1320\n"
     ]
    }
   ],
   "source": [
    "def hb_generator(task_num, total_minutes, min:int = 20):\n",
    "    task_remain = int(task_num)\n",
    "    for i in range(int(task_num) -1):\n",
    "        #min = 10\n",
    "        max = total_minutes/task_remain*2\n",
    "        minute = int(round(random.uniform(0, max), 2))\n",
    "        task_minute= minute if minute > min else min\n",
    "        task_remain -= 1\n",
    "        total_minutes -= task_minute\n",
    "        yield task_minute\n",
    "    yield round(int(total_minutes), 2)\n",
    "\n",
    "total = 0\n",
    "for minute in hb_generator(40, 1320):\n",
    "    total += minute\n",
    "    print(minute)\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def process_wechat(device):\n",
    "    print(device)\n",
    "    try:\n",
    "        d = u2.connect(device)\n",
    "       \n",
    "        d.app_start('com.tencent.mm')\n",
    "        # 读取群成员好友列表。\n",
    "       \n",
    "        d(resourceId=\"com.tencent.mm:id/cns\", text=\"通讯录\").click()\n",
    "\n",
    "        d(resourceId=\"com.tencent.mm:id/fx\", text=\"群聊\").click()\n",
    "\n",
    "        d.xpath('//*[@resource-id=\"com.tencent.mm:id/f4\"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]').click()\n",
    "\n",
    "        #d.press(\"back\")\n",
    "        d.xpath('//*[@resource-id=\"com.tencent.mm:id/rr\"]').click_exists()\n",
    "\n",
    "        d(resourceId=\"com.tencent.mm:id/cns\", text=\"通讯录\").click()\n",
    "\n",
    "        d(resourceId=\"com.tencent.mm:id/fx\", text=\"群聊\").click()\n",
    "\n",
    "        d.xpath('//*[@resource-id=\"com.tencent.mm:id/f4\"]/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]').click()\n",
    "\n",
    "        d.xpath('//*[@resource-id=\"com.tencent.mm:id/rr\"]').click_exists()\n",
    "    except:\n",
    "        print(\"went wrong\")\n",
    "\n",
    "devices = ['15793cac0806']\n",
    "\n",
    "for device in devices:\n",
    "    # process_wechat(device=device)\n",
    "    # print(device)\n",
    "    p = multiprocessing.Process(target=process_wechat, args=(device,))\n",
    "    p.start()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}